package com.example.final_project.Cate.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.final_project.Cate.Activity.CateCarActivity;
import com.example.final_project.Cate.entry.CarSign;
import com.example.final_project.R;

import java.util.List;

public class SignAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<CarSign> carSigns;

    public SignAdapter(Context context, List<CarSign> carSigns){
        this.context = context;
        this.carSigns = carSigns;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.cate_sign_kid,parent,false);
        SignHolder signHolder = new SignHolder(view);
        return signHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        SignHolder signHolder = (SignHolder)holder;
        signHolder.setDatas(position);
        if(onItemClickListener != null){
            signHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.OnItemClick(v,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return carSigns.size();
    }

    class SignHolder extends RecyclerView.ViewHolder {
        public SignHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setDatas(int position){
            CarSign carSign = carSigns.get(position);
            TextView index = itemView.findViewById(R.id.cate_car_index);
            if(position == 0 || !carSign.getIndex().equals(carSigns.get(position -1).getIndex())){
                index.setVisibility(View.VISIBLE);
                index.setText(carSign.getIndex());
            }
            else {
                index.setVisibility(View.GONE);
            }
            ImageView imageView = itemView.findViewById(R.id.cate_car_sign);
            Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+carSign.getImg()).into(imageView);
            TextView textView = itemView.findViewById(R.id.cate_car_name);
            textView.setText(carSign.getName());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, CateCarActivity.class);
                    intent.putExtra("name",carSign.getName());
                    context.startActivity(intent);
                }
            });
        }

    }

    public interface OnItemClickListener{
        void OnItemClick(View view,int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

}
